<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="168px">
<!--      <el-form-item label="" prop="natureday">-->
<!--        <el-input-->
<!--          v-model="queryParams.natureday"-->
<!--          placeholder="请输入"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="三位本币币种代码" prop="originalcurrency">
        <el-input
          v-model="queryParams.originalcurrency"
          placeholder="请输入三位本币币种代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三位外币币种代码" prop="valuationcurrency">
        <el-input
          v-model="queryParams.valuationcurrency"
          placeholder="请输入三位外币币种代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="" prop="exchangerate">-->
<!--        <el-input-->
<!--          v-model="queryParams.exchangerate"-->
<!--          placeholder="请输入"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          @click="handleAdd"
        >导入</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exchangerateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="今日日期" align="center" prop="natureday" />
      <el-table-column label="三位本币币种代码" align="center" prop="originalcurrency" />
      <el-table-column label="三位外币币种代码" align="center" prop="valuationcurrency" />
      <el-table-column label="汇率" align="center" prop="exchangerate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['operation:exchangerate:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['operation:exchangerate:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script setup name="Exchangerate">
import { importData,listExchangerate } from "@/api/operation/exchangerate";

const { proxy } = getCurrentInstance();

const exchangerateList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    natureday: undefined,
    originalcurrency: undefined,
    valuationcurrency: undefined,
    exchangerate: undefined
  },
  rules: {
    natureday: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    originalcurrency: [
      { required: true, message: "三位本币币种代码不能为空", trigger: "blur" }
    ],
    valuationcurrency: [
      { required: true, message: "三位外币币种代码不能为空", trigger: "blur" }
    ],
    exchangerate: [
      { required: true, message: "不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询来自finchina的汇率列表 */
function getList() {
  loading.value = true;
  listExchangerate(queryParams.value).then(response => {
    exchangerateList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    natureday: null,
    originalcurrency: null,
    valuationcurrency: null,
    exchangerate: null
  };
  proxy.resetForm("exchangerateRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.natureday);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  importData().then(res=>{
    proxy.$modal.msgSuccess(res.msg);
    getList();
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _natureday = row.natureday || ids.value
  getExchangerate(_natureday).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改来自finchina的汇率";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["exchangerateRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.natureday != null) {
        updateExchangerate(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addExchangerate(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _naturedays = row.natureday || ids.value;
  proxy.$modal.confirm('是否确认删除来自finchina的汇率编号为"' + _naturedays + '"的数据项？').then(function() {
    loading.value = true;
    return delExchangerate(_naturedays);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('operation/exchangerate/export', {
    ...queryParams.value
  }, `exchangerate_${new Date().getTime()}.xlsx`)
}

getList();
</script>
